package conceptmaniax.hotelapp.util.database;

import static conceptmaniax.hotelapp.config.Constants.DATABASE_VERSION;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import conceptmaniax.hotelapp.util.sync.FileSyncUtils;

public class DataHelper extends SQLiteOpenHelper {
	private SQLiteDatabase dataBase;
	private final Context context;
	private final String name;

	public DataHelper(final Context context, String name) {
		super(context, name, null, DATABASE_VERSION);
		this.name = name;
		this.context = context;
	}

	public void createDataBase(final boolean upgrade, String name) throws IOException {
		if (!dataBaseExists() || upgrade) {			
			this.getReadableDatabase();
			this.copyDataBaseFromAssets();
		}
	}
	
	public boolean dataBaseExists() {
		try {
			dataBase = SQLiteDatabase.openDatabase(this.context.getDatabasePath(name).getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
		} catch (Exception e) {
			return false;
		} finally {
			close();
		}
		
		return true;
	}

	public void copyDataBaseFromAssets() throws IOException {
		// call database exists to create databases folder 
		this.getReadableDatabase();
		
		// make the actual copy
		final InputStream myInput = this.context.getAssets().open(name);
		final OutputStream myOutput = new FileOutputStream(this.context.getDatabasePath(name).getAbsolutePath());
		FileSyncUtils.copyFile(myInput, myOutput);
	}

	public void openDatabase(boolean write) throws SQLException {
		if (write) {
			dataBase = SQLiteDatabase.openDatabase(this.context.getDatabasePath(name).getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
		} else {
			dataBase = SQLiteDatabase.openDatabase(this.context.getDatabasePath(name).getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
		}
	}

	public SQLiteDatabase getDataBase() {
		return dataBase;
	}

	public void setDataBase(final SQLiteDatabase dataBase) {
		this.dataBase = dataBase;
	}

	public Context getContext() {
		return context;
	}

	@Override
	public synchronized void close() {
		if (dataBase != null && dataBase.isOpen()) {
			dataBase.close();
		}
		super.close();
	}
	
	public Cursor getData(final String query) {	
		this.openDatabase(false);
		SQLiteDatabase database = getDataBase();
		Cursor cursor = database.rawQuery(query, null);	
		return cursor;
	}
	
	public Cursor getData(final String query, boolean databaseIsAlreadyOpen) {
		return dataBase.rawQuery(query, null);
	}
	
	public void attachData(final String db2) {	
		String attachedDatabase = this.context.getDatabasePath(db2).getAbsolutePath();
		dataBase.execSQL("ATTACH database '"+attachedDatabase +"' AS db2");
	}

	@Override
	public void onCreate(final SQLiteDatabase database) { }

	@Override
	public void onUpgrade(final SQLiteDatabase database, final int oldVersion, final int newVersion) { }
}
